from selenium import webdriver
from lxml import etree
import re
import json
import time

# pattern = re.compile('[^0-9\.]')
#
# url = 'http://daoju.qq.com/lol/list/17-0-0-0-0-0-0-0-0-0-0-00-0-0-1-1.shtml?ADTAG=innercop.lol.SY.shoppinglist_new_17'
# driver = webdriver.Chrome()
# driver.get(url)
#
# file = open('lol_skins.txt', 'a')
# next_page = 1
# while False:
#     print('crawl {} page'.format(next_page))
#     html = driver.page_source
#     element = etree.HTML(html)
#     for dl in element.xpath('//div[@id="blk_list_list"]/dl'):
#         img = dl.xpath('./dt/a/img/@src')[0]
#         name = dl.xpath('./dd/a/strong/text()')[0]
#         qb_price = dl.xpath('./dd/div[1]/span[2]/text()')[0]
#         wx_price = dl.xpath('./dd/div[2]/span/text()')[0]
#         item = dict(img=img, name=name, qb_price=qb_price, wx_price=pattern.sub('', wx_price))
#         file.writelines(json.dumps(item) + '\n')
#
#     # 检测下一页
#     if int(next_page) == 75:
#         print('over')
#         file.close()
#         driver.quit()
#         break
#     next_page_element = driver.find_element_by_css_selector(".next_on_btn.page_bg")
#     next_page = next_page_element.get_attribute('data-number')
#     next_page_element.click()
#     time.sleep(2)

# 分析
file = open('lol_skins.txt', 'r')
skins =[json.loads(skin) for skin in file.readlines()]

skins.sort(key=lambda x: float(x['qb_price']), reverse=True)
skins_num = len(skins)
total_price = sum([float(skin['qb_price']) for skin in skins])
avg_price = total_price/skins_num

print('现在上架可以购买的皮肤一共有 {} 款'.format(skins_num))
print('售价最高皮肤为 {},qb价格为 {} 元'.format(skins[0]['name'], skins[0]['qb_price']))
print('售价最低皮肤为 {},qb价格为 {} 元'.format(skins[skins_num-1]['name'], skins[skins_num-1]['qb_price']))
print('皮肤总价为 {} 元,平均qb价格为 {} 元'.format(total_price, avg_price))

new_skins = [skin for skin in skins if float(skin['qb_price'])<=10]
print(new_skins.__len__())
for skin in new_skins:
    print(skin)








